<template>
  <common-view class="lottery-page">
    <design-view>
      <div class="lottery-bg">
        <img src="../assets/img/lottery_light.png" class="light" alt=""/>
        <img src="../assets/img/lottery_container_attachment.png" class="attachment" alt=""/>
      </div>
      <div class="lottery-container">
        <img v-if="my.brand == 0" src="../assets/img/award/lottery_he_title.png" class="title" alt=""/>
        <img v-else src="../assets/img/award/lottery_kou_title.png" class="title" alt=""/>
        <div class="lottery-panel">
          <LuckyGrid
            ref="myLucky"
            :width="lotteryWidth"
            :height="lotteryHeight"
            rows="4"
            cols="4"
            :prizes="lotteryPanel"
            :blocks="blocks"
            :defaultStyle="defaultStyle"
            :defaultConfig = "defaultConfig"
            @start="startCallback"
            @end="endCallback"
          />
        </div>
        <div class="text-tip">
          <van-button :disabled="isRunning" class="text"  @click="openRule">抽奖规则</van-button>
        </div>
        <van-button :disabled="isRunning" class="btn-lottery  animate__animated animate__headShake animate__infinite" @click="startCallback">开始抽奖</van-button>
        <van-notice-bar class="lottery_list_container" v-if="lotteries && lotteries.length">
          <van-swipe
            vertical
            class="notice-swipe"
            :autoplay="3000"
            :show-indicators="false"
          >
            <van-swipe-item class="lottery_item" v-for="(item, index) in lotteries" :key="item.mobile + index">
              恭喜{{item.mobile }}用户{{ item.name }}礼品
            </van-swipe-item>
          </van-swipe>
        </van-notice-bar>
      </div>
    </design-view>
    <van-popup
      transition = "fade"
      class="popup"
      position="center"
      v-model="showDialog">
      <component :is="popup" @show-form="toggleDialog('formView')" @close-dialog="toggleDialog" @next-page="toNextPage"></component>
    </van-popup>
  </common-view>
</template>

<script>
import { Popup, Button, NoticeBar, Swipe, SwipeItem, Dialog } from 'vant'
import { mapGetters, mapState } from 'vuex'
import AwardPopup from '@/components/AwardPopup'
import LotteryRulePopup from '@/components/LotteryRulePopup'
import NoAwardPopup from '@/components/NoAwardPopup'
import LotteryFormView from '@/views/LotteryFormView.vue'
import { UPDATE_PRIZE } from '@/store/mutation-types'
import { getLottery } from '@/common/api'
export default {
  name: 'LotteryView',
  components: {
    [Popup.name]: Popup,
    [NoticeBar.name]: NoticeBar,
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    award: AwardPopup,
    rule: LotteryRulePopup,
    noAward: NoAwardPopup,
    formView: LotteryFormView,
    [Button.name]: Button
  },
  data () {
    return {
      showDialog: false,
      popup: '',
      isRunning: false,
      blocks: [
      ],
      defaultConfig: {
        gutter: 300 / 750 + 'vw'
      },
      defaultStyle: {
        borderRadius: 5 / 750 * 1000 + 'vew'
      },
      lists: []
    }
  },
  computed: {
    ...mapState(['savedInfo', 'prizeType', 'my', 'isSaved', 'lotteries']),
    ...mapGetters(['lotteryPanel', 'prize']),
    lotteryWidth () {
      return 558 / 750 * 100 + 'vw'
    },
    lotteryHeight () {
      return 561 / 750 * 100 + 'vw'
    },
    lotteryTitle () {
      return ''
    }
  },
  mounted () {
    if (this.prizeType > 0 && !this.isSaved) {
      this.toggleDialog('formView')
    }
  },
  methods: {
    async startCallback () {
      this.$sound && this.$sound.play()
      // 调用抽奖组件的play方法开始游戏
      const { status, data, message } = await getLottery({ brand: this.my.brand })
      if (status < 0) {
        Dialog.alert({ message })
        return
      }
      this.isRunning = true
      this.$refs.myLucky.play()
      this.$store.commit(UPDATE_PRIZE, status === 0 ? 0 : data.type)
      const index = status === 0 ? 3 : this.prize.indexes
      setTimeout(() => {
        this.$refs.myLucky.stop(index)
      }, 3000)
    },
    // 抽奖结束会触发end回调
    endCallback () {
      this.popup = this.prizeType > 0 ? 'award' : 'noAward'
      this.showDialog = true
      this.isRunning = false
    },
    // 切换弹出窗
    toggleDialog (name = '') {
      this.popup = name
      this.showDialog = !!name
    },
    toNextPage () {
      this.toggleDialog('')
      this.$emit('next-page')
    },
    openRule () {
      this.$sound && this.$sound.play()
      this.toggleDialog('rule')
    }
  }
}
</script>

<style scoped lang="scss">
@import "../assets/css/mixin.scss";
.lottery-page {
  background: url(../assets/img/lottery_bg.jpg) center center;
  background-size: cover;
}
.lottery-bg,
.lottery-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-size: 0;
}
.light {
  width: 100%;
}
.attachment {
  @include wh(617, 30);
  position: absolute;
  left: 0;
  right: 0;
  margin: 0 auto;
  top: 338px;
}
.title {
  @include wh(589, 208);
  margin: 77px auto 0;
}
.lottery-panel {
  @include wh(657, 663);
  background: url(../assets/img/award/lottery_panel_bg.png) center no-repeat;
  background-size: cover;
  margin: 76px auto 37px;
  padding: 46px 47px 56px 52px;
}
.text-tip {
  font-size: 28px;
  line-height: 1;
  color: #fff;
  margin: 0 auto 22px;
  .text {
    height: auto;
    color: white;
    background-color: transparent;;
    text-decoration: underline;
    padding: 0;
    line-height: 1;
    border: none;
    font-size: 28px;
    ::v-deep(.van-button--normal) {
      padding: 0;
    }
  }
}
.btn-lottery {
  @include wh(479, 88);
  background: url(../assets/img/second/btn_bg.png) center no-repeat;
  background-size: 100% 100%;
  margin:  0 auto;
  font-size: 40px;
  color: white;
  line-height: 88px;
  text-align: center;
  border: none;
}
.lottery_list_container {
  display: block;
  background: #780c0b;
  height: 45px;
  line-height: 45px;
  font-size: 24px;
  color: #faf2bb;
  border-radius: 24px;
  margin: 37px 30px 0;
  border: 2px solid #faf2bb;
}
.notice-swipe {
  height: 45px;
  line-height: 45px;
  color: #faf2bb;
}
.lottery_list_container,
.notice-swipe,
.lottery_item {
  text-align: center;
  align-items: center;
  text-align: center;
  ::v-deep(.van-notice-bar__wrap) {
    justify-content: center;
  }
}
.small {
  .attachment {
    top: 318px;
  }
  .lottery-panel {
    margin: 56px auto 17px;
  }
}
</style>
